package com.zl.learning.maven.shade.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

public class Env extends Properties {

    private static Env instance = new Env();

    public static Env getInstance() {
        return instance;
    }

    private Env() {
        try {
            this.load(new InputStreamReader(getResourceAsStream("prop.conf"), "GBK"));
            this.load(getResourceAsStream("server.conf"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private InputStream getResourceAsStream(String file) {
        return getDefaultClassLoader().getResourceAsStream(file);
    }

    private URL getResource(String file) {
        return getDefaultClassLoader().getResource(file);
    }

    private ClassLoader getDefaultClassLoader() {
        ClassLoader cl = null;
        try {
            cl = Thread.currentThread().getContextClassLoader();
        } catch (Throwable ignored) {

        }
        if (cl == null) {

            cl = Env.class.getClassLoader();
            if (cl == null) {
                try {
                    cl = ClassLoader.getSystemClassLoader();
                } catch (Throwable ignored) {

                }
            }
        }
        return cl;
    }
}
